/**
 *  @file Entities.java
 *  @author Mamontov
 *
 *  This file contains Entities class definition
 */
package db21.entities;


import com.hp.hpl.jena.ontology.Individual;
import db21.KBConnection;
import db21.racerpro.Queries;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 * @class  Entities
 * @author Mamontov
 *
 *
 */
public class Entities {
    protected KBConnection m_conn;
    protected String m_conceptname;
    public Entities() {
        
    }
    
    public ArrayList<String> getList()  {
        String individuals = m_conn.buildQuery(Queries.individualsOfType(), m_conceptname);
        return m_conn.asResult(m_conn.evaluateSparQL(individuals)).asRow();
    }
    
    public void set(KBConnection conn) {
        m_conn = conn;
    }

    public Individual makeAndReturnIndividual(String conceptname, String name) {
        Individual m =  m_conn.createIndividual(conceptname, name);
        return m;
    }
    
    public boolean makeIndividual(String name)  {
        Individual m =  makeAndReturnIndividual(m_conceptname, name);
        if (m != null) {
            m_conn.syncWithRacerPro();
        }
        return m != null;
    }
    
    public void removeIndividual(String name)  {
        m_conn.removeIndividual(name);
        m_conn.syncWithRacerPro();
    }
   
    public static void populateListWithStrings(JList c, ArrayList<String> a) {
        DefaultListModel m = (DefaultListModel)(c.getModel());        
        m.removeAllElements();
        for(int i = 0 ; i < a.size(); i++) {
            m.addElement(a.get(i));
        }
    }
    
    public static void populateComboBoxWithStrings(JComboBox c, ArrayList<String> a) {
        c.removeAllItems();
        for(int i = 0 ; i < a.size(); i++) {
            c.addItem(a.get(i));
        }
    }
    
    public void populateComboBoxWithEntities(JComboBox cmb) {
        populateComboBoxWithStrings(cmb, this.getList());
    }
    
    public static int findComboBoxItem(JComboBox item, String s) {
        int result =  -1;
        int numOfMenus = item.getItemCount();
  
        for ( int i = 0;  i < numOfMenus; i++) {
            String val = (String)(item.getItemAt(i));
            if (val.equals(s)) {
                result =  i;
            }
        }
        
        return result;
    }
    
    public static int findListItem(JList list, String s) {
        int result = -1;
        DefaultListModel m = (DefaultListModel)(list.getModel());
        for(int i = 0; i < m.size(); i++) {
            String l = m.get(i).toString();
            if (l.equals(s)) {
                result = i;
            }
        }
        return result;
    }
    
    public static ArrayList<Integer> intersectItemsWithStrings(JList l, ArrayList<String> l2) {
        ArrayList<Integer>  result = new ArrayList<Integer> ();
        for(int i = 0; i < l2.size(); i++) {
            int index = Entities.findListItem(l, l2.get(i));
            if (index != -1) {
                result.add(index);
            }
        }
        return result;
    }
    
    public static void setSelectedValues(JList  l, ArrayList<String> l2) {
        ArrayList<Integer> list = intersectItemsWithStrings(l, l2);
        int k[] = new int[list.size()];
        for(int i = 0; i < k.length; i++) {
            k[i] = list.get(i);
        }
        l.setSelectedIndices(k);
    }
    
    public void populateListWithEntities(JList cmb) {
        populateListWithStrings(cmb, this.getList());
    }
    
    public static ArrayList<String> selectedItemsToEntities(JList src) {
        ArrayList<String>  result = new ArrayList<String>();
        Object os[] = src.getSelectedValues();
        for(int i = 0; i < os.length; i++) {
            result.add(os[i].toString());
        }
        return result;
    }
    
    protected ArrayList<String> fetchAsRow(String query) {
        return m_conn.asResult(m_conn.evaluateSparQL(query)).asRow();
    }
    
}
